<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    title="字段配置"
    width="80%"
  >
    <template #footer>
      <a-button @click="closeModal">关闭</a-button>
    </template>
    <BasicTable @register="registerTable" @row-drag-end="handleDragEnd">
      <template #toolbar>
        <a-button
          type="primary"
          v-if="hasPermission(['sysExportConfig:add'])"
          @click="handleCreate"
        >
          新增
        </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              auth: 'sysExportConfig:edit',
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              auth: 'sysExportConfig:delete',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <EditModal @register="registerEditModal" @success="handleSuccess" />
  </BasicModal>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { usePermission } from '@/hooks/web/usePermission';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { BasicModal, useModal, useModalInner } from '@/components/Modal';
import { columns } from './data';
import EditModal from './EditModal.vue';
import { list, del } from '@/api/sys/sys/sysExportConfigDetailManage';
import { useMessage } from '@/hooks/web/useMessage';
import { edit, add } from '@/api/sys/sys/sysExportConfigDetailManage';

const { hasPermission } = usePermission();
const emit = defineEmits(['success']);
// 配置主表的id
const configId = ref('');
const { createMessage } = useMessage();

const [registerTable, { reload, setTableData, getDataSource }] = useTable({
  title: '列表',
  api: list,
  columns,
  useSearchForm: false,
  showTableSetting: true,
  bordered: true,
  pagination: false,
  showIndexColumn: false,
  beforeFetch: (params) => {
    params.secId = configId.value;
    return params;
  },
  actionColumn: {
    width: 200,
    title: '操作',
    dataIndex: 'action',
    slots: { customRender: 'action' },
    fixed: 'right',
  },
});
const [registerEditModal, { openModal }] = useModal();

const [registerModal, { setModalProps, closeModal }] = useModalInner(
  async (data) => {
    setModalProps({ confirmLoading: false });
    configId.value = data?.id;
    reload();
  },
);

const handleCreate = () => {
  openModal(true, {
    isUpdate: false,
    secId: configId.value,
  });
};
const handleEdit = (record: Recordable) => {
  openModal(true, {
    record,
    isUpdate: true,
    secId: configId.value,
  });
};
const handleDelete = (record: Recordable) => {
  del([record.id]).then(() => {
    createMessage.success(`删除成功`);
    reload();
  });
};
const handleSuccess = () => {
  reload();
};
const handleDragEnd = (dragElement, nIndex) => {
  edit({ ...dragElement, secId: configId.value, secdOrder: nIndex + 1 })
    .then(() => {
      createMessage.success('保存成功');
    })
    .catch(() => createMessage.error('保存失败'));
};
</script>
